package de.firefligher.ballergame.gui;

import java.io.IOException;

import org.lwjgl.opengl.GL11;
import org.newdawn.slick.opengl.Texture;
import org.newdawn.slick.opengl.TextureLoader;

import de.firefligher.ballergame.BallerGame;

public class GuiMainMenu extends Gui implements Runnable {
	private Texture[] texture;
	private float angleSun;
	private Thread worldTick;
	private boolean isDay;
	private float rotAngleSun;
	private float bgGreen;
	private float bgBlue;
	private boolean goDark;
	
	public GuiMainMenu() {
		try {
			texture = new Texture[] {
					TextureLoader.getTexture("PNG", BallerGame.class.getResourceAsStream("/de/firefligher/ballergame/textures/sun.png")),
					TextureLoader.getTexture("PNG", BallerGame.class.getResourceAsStream("/de/firefligher/ballergame/textures/moon.png")),
					TextureLoader.getTexture("PNG", BallerGame.class.getResourceAsStream("/de/firefligher/ballergame/textures/title.png"))
			};
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		angleSun = 0F;
		worldTick = new Thread(this);
		isDay = true;
		rotAngleSun = 0F;
		bgGreen = 0F;
		bgBlue = 0F;
		goDark = false;
		
		
		worldTick.start();
	}
	
	@Override
	public void render() {
		
		updateBackground();
		
		GL11.glTranslatef(-(float)Math.cos(angleSun) * 6, (float)Math.sin(angleSun) * 6, -20f);
		GL11.glRotatef(rotAngleSun, 0F, 0F, 1F);
		
		if(isDay) {
			texture[0].bind();
		} else {
			texture[1].bind();
		}
		
		GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MIN_FILTER, GL11.GL_NEAREST);
		GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MAG_FILTER, GL11.GL_NEAREST);
		
		GL11.glColor4f(1F, 1F, 1F, 1.0F);
		
		GL11.glBegin(GL11.GL_QUADS);
			GL11.glTexCoord2f(0, 0);
			GL11.glVertex3f(1F, 1F, 1F);
			GL11.glTexCoord2f(1, 0);
			GL11.glVertex3f(-1F, 1F, 1F);
			GL11.glTexCoord2f(1, 1);
			GL11.glVertex3f(-1F, -1F, 1F);
			GL11.glTexCoord2f(0, 1);
			GL11.glVertex3f(1F, -1F, 1F);
        GL11.glEnd();
        
        drawForeground();
	}

	private void drawForeground() {
		GL11.glLoadIdentity();

		GL11.glDisable(GL11.GL_TEXTURE_2D);
		
		GL11.glTranslatef(0F, 0F, -6F);
		GL11.glColor4f(1F, 1F, 1F, 0.5F);
		
		GL11.glBegin(GL11.GL_QUADS);
			GL11.glVertex3f(1F, 1.5F, 1F);
			GL11.glVertex3f(-1F, 1.5F, 1F);
			GL11.glVertex3f(-1F, -1.5F, 1F);
			GL11.glVertex3f(1F, -1.5F, 1F);
		GL11.glEnd();
		
		
		
		GL11.glEnable(GL11.GL_TEXTURE_2D);
		
		GL11.glColor4f(1F, 1F, 1F, 1.0F);
		
		texture[2].bind();
		GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MIN_FILTER, GL11.GL_NEAREST);
		GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MAG_FILTER, GL11.GL_NEAREST);
		
		GL11.glBegin(GL11.GL_QUADS);
			GL11.glTexCoord2f(1, 0);
			GL11.glVertex3f(1F, 1.5F, 1F);
			GL11.glTexCoord2f(0, 0);
			GL11.glVertex3f(-1F, 1.5F, 1F);
			GL11.glTexCoord2f(0, 1);
			GL11.glVertex3f(-1F, 0.5F, 1F);
			GL11.glTexCoord2f(1, 1);
			GL11.glVertex3f(1F, 0.5F, 1F);
		GL11.glEnd();
	}

	@Override
	public void run() {
		while(BallerGame.isRunning) {
			try {
				Thread.sleep(1);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			tickInWorld();
		}
	}
	
	public void updateBackground() {
		if(!isDay)
			GL11.glClearColor(0F, 0F, 0F, 0F);
		else
			GL11.glClearColor(0F, bgGreen, bgBlue, 0F);
	}

	private void tickInWorld() {
		angleSun += 0.001F;
		rotAngleSun += 0.1F;
		
		if(bgGreen < 0.749F && !goDark) {
			bgGreen += 0.0007F;
		}
		
		if(bgGreen > 0F && goDark) {
			bgGreen -= 0.0006F;
		}
		
		if(bgBlue < 1.0F && !goDark) {
			bgBlue += 0.0007F;
		} else {
			goDark = true;
		}
		
		if(bgBlue > 0F && goDark) {
			bgBlue -= 0.0006F;
		}
		
		if(angleSun >= 3) {
			isDay = !isDay;
			angleSun = 0F;
			bgGreen = 0F;
			bgBlue = 0F;
			goDark = false;
		}
	}
	
	@Override
	public void mouseClick(int x, int y, int button) {
		System.out.println("Shoot at '" + x + ", " + y + "!");
	}
}
